//
//  MineTableViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/5/30.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "MineTableViewController.h"
#import "MineHeaderView.h"
#import "MineFirstCell.h"
#import "MineSecondCell.h"
#import "MyAccountTableViewController.h"
#import "AccountSetupController.h"
#import "BaseNavigationController.h"
#import "LogInViewController.h"
#import "SetupController.h"
#import "MyStandingsViewController.h"
#import "IndividualTableViewController.h"
#import "SellerPreFectureTableViewController.h"
#import "SetupController.h"
#import "SuggestionsController.h"
#import "MyAssertViewCell.h"
#import "MyOrderMainController.h"
#import "MyCollectionController.h"

//我的订单
#import "GoodsOrderViewController.h"
#import "OilOrderViewController.h"
#import "CoastOrderViewController.h"
#import "ActivityOrderViewController.h"

#import "PayMoneyController.h"

/**
 *  判断个人或商家专区
 */
#define JudgeInvididualOrSeller ([ReadLoginData readLoginData].allKeys.count == 0)||([[[ReadLoginData readLoginData] objectForKey:@"lb"] isEqualToString:@"0"])||([[[ReadLoginData readLoginData] objectForKey:@"lb"] isEqualToString:@"1"])

@interface MineTableViewController ()<MineHeaderViewDelegate,MineSecondCellDelegate,UIAlertViewDelegate,MyAssertViewCellDelegate>
{
    NSArray *mineArr;
    NSString *userNameString;
}
@property (nonatomic,strong)NSDictionary *userInfoDict;

@property (nonatomic,strong)MineHeaderView *headView;
@end

@implementation MineTableViewController

/**
 *  提交测试
 */
static NSString *const mineFirstId = @"mineFirstId";

- (void)loadView {
    [super loadView];
    
    if (JudgeLogInData) {
        [self loadUserInfoNumberData];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    if (JudgeLogInData) {
        NSLog(@"%@",[ReadLoginData readLoginData]);
        [self loadUserInfoNumberData];
    } else {
        [self.tableView reloadData];
    }
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
 
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    mineArr = @[@"我的亿乐账户",@"我的积分",@"活动记录",@"个人专区",@"我的收藏",@"意见反馈"];
    self.tableView.showsVerticalScrollIndicator = NO;
}


#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 2;
    }else if (section == 2){
        return 5;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MineFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:mineFirstId];
    
    if (cell==nil) {
        
        cell = [[MineFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mineFirstId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor blackColor];
        
    }
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            //我的资产
            cell.mineImg.image = [UIImage imageNamed:@"wodezichan_0"];
            cell.mineLabel.text = @"我的资产";
           
            //充值
            [cell.PayBtn setTitle:@"充值" forState:UIControlStateNormal];
            cell.PayBtn.titleLabel.font = FONT(15);
            [cell.PayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cell.PayBtn setImage:[UIImage imageNamed:@"icon_PayBtn"] forState:UIControlStateNormal];
//            cell.PayBtn.contentMode = UIViewContentModeCenter;
            cell.PayBtn.imageEdgeInsets = UIEdgeInsetsMake(0,-20,0,0);
            [cell.PayBtn addTarget:self action:@selector(payBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
            
            return cell;
        }else{
        
            MyAssertViewCell *cell = [[MyAssertViewCell alloc] init];
            cell.delegate = self;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (JudgeLogInData && self.userInfoDict != 0) {
                cell.userInfoNumberData = self.userInfoDict;
            }
            cell.logInDic = [ReadLoginData readLoginData];
            return cell;

        }
    
    }else if(indexPath.section == 1 ){
        
        if (indexPath.row == 0) {
        
            //我的订单
            cell.mineImg.image = [UIImage imageNamed:@"wode_1"];
            cell.mineLabel.text = @"我的订单";
            return cell;
            
        }else{
            MineSecondCell *cell = [[MineSecondCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
            return cell;
        }

    }else if (indexPath.section == 2){
        /**
         *  <#Description#>
         */
        cell.mineImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"wodeSection2_%ld",indexPath.row]];
        cell.mineLabel.text = [NSString stringWithFormat:@"%@",mineArr[indexPath.row]];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        if(indexPath.row == 3) {
            if (JudgeInvididualOrSeller) {
                cell.mineLabel.text = @"个人专区";
            } else {
                cell.mineLabel.text = @"商家专区";
            }
        }
        
        return cell;
    
    }else{
        
        cell.mineImg.image = [UIImage imageNamed:@"wodeSection2_5"];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        cell.mineLabel.text = @"意见反馈";
        return cell;
    }

}


#pragma mark>>>>>>>>>>>>代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //用户是否登录
    if ([JudgeUserLogInTool JudgeUserLonInStatus:self.view]) {
        
        if (indexPath.section == 0) {
            
            
        }else if (indexPath.section == 1){
            
            //我的订单
            
        }else if (indexPath.section == 2){
            
            if (indexPath.row == 0) {
                //我的亿乐账户
                MyAccountTableViewController *myAccount = [[MyAccountTableViewController alloc] init];
                myAccount.userNameString = [self.userInfoDict[@"data"] objectForKey:@"xingming"];
                [self.navigationController pushForm:self.navigationController pushToAndHidden:myAccount];
                
            }else if (indexPath.row == 1){
                //我的积分
                MyStandingsViewController *myStandings = [[MyStandingsViewController alloc] init];
                myStandings.nameLabelString = [self.userInfoDict[@"data"] objectForKey:@"xingming"];
                [self.navigationController pushForm:self.navigationController pushToAndHidden:myStandings];
                
            }else if (indexPath.row == 2){
                //活动记录
                
            }else if (indexPath.row == 3){
#warning 商家个人专区却换
                if (JudgeInvididualOrSeller) {
                    //个人专区
                    IndividualTableViewController *individual = [[IndividualTableViewController alloc] init];
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:individual];
                } else {
                    //商家专区
                    SellerPreFectureTableViewController *seller = [[SellerPreFectureTableViewController alloc] init];
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:seller];
                }
            }else{
                //我的收藏
                
                MyCollectionController *MyCollectionVC = [[MyCollectionController alloc] init];
                MyCollectionVC.title = @"我的收藏";
                [self.navigationController pushForm:self.navigationController pushToAndHidden:MyCollectionVC];
            }
            
        }else{
            //意见反馈
            SuggestionsController * sugges = [[SuggestionsController alloc] init];
            [self.navigationController pushForm:self.navigationController pushToAndHidden:sugges];
        }
        
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您尚未登录用户" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *SettingAction = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            LogInViewController *accountVC = [[LogInViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:accountVC];
            [self presentViewController:nav animated:YES completion:nil];
        }];
        [alert addAction:SettingAction];
        
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];

    }
    
    NSLog(@"%ld",indexPath.row);
    NSLog(@"%ld",indexPath.section);
}

-(void)didSelectedSwitchViewController:(UITableViewController *)controller
{
    UITableViewController *VC = [[UITableViewController alloc] init];
    VC = controller;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:controller];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            return HEIGHT/14;
        }else{
            return HEIGHT/10;
        }
        
    }else if(indexPath.section == 1){
        
        if (indexPath.row == 0) {
            return HEIGHT/14;
        }else{
            return HEIGHT/10;
        }
    }else{
        return HEIGHT/14;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return HEIGHT/5;
    }
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        MineHeaderView *headView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
        headView.delegate = self;
        self.headView = headView;
        return headView;
    }
    return nil;
}

#pragma mark >>>>>>>>>>>头部视图代理方法
- (void)mineHeaderDidClick
{
    if ([ReadLoginData readLoginData].count != 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"退出用户登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        
        alert.tag = 1001;
        
        [alert show];
        
    } else {
        LogInViewController *accountVC = [[LogInViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:accountVC];
        [self presentViewController:nav animated:YES completion:nil];

    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1001) {
        if (buttonIndex == 0) {
            [ReadLoginData cleanloginData];
            
            [self.tableView reloadData];
        }
    }
}

//设置
- (void)mineHeaderSetupDidClick
{
    SetupController *setupVc = [[SetupController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:setupVc];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"%f",self.tableView.y);
}

//充值
- (void)payBtnDidClick
{
    
    //用户是否登录
    if (![JudgeUserLogInTool JudgeUserLonInStatus:self.view]) {
        return;
    }
    
    PayMoneyController *PayMoneyVC = [[PayMoneyController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:PayMoneyVC];
}

#pragma mark >>>>>>>>>>>我的订单delegate
- (void)mineSecondCellDidClickIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    
    //用户是否登录
    if (![JudgeUserLogInTool JudgeUserLonInStatus:self.view]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您尚未登录用户" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *SettingAction = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            LogInViewController *accountVC = [[LogInViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:accountVC];
            [self presentViewController:nav animated:YES completion:nil];
        }];
        [alert addAction:SettingAction];
        
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];
        return;
    }

    if (index == 100) {
        //商品
        GoodsOrderViewController *GoodsOrderVC = [[GoodsOrderViewController alloc] init];
        [self.navigationController pushForm:self.navigationController pushToAndHidden:GoodsOrderVC];
        
    }else if (index == 101){
        //加油卡
        OilOrderViewController *OilOrderVC = [[OilOrderViewController alloc] init];
        [self.navigationController pushForm:self.navigationController pushToAndHidden:OilOrderVC];
        
    }else if (index == 102){
        //话费
        CoastOrderViewController *CoastOrderVC = [[CoastOrderViewController alloc] init];
        [self.navigationController pushForm:self.navigationController pushToAndHidden:CoastOrderVC];
        
    }else{
        //活动
        ActivityOrderViewController *ActivityOrderVC = [[ActivityOrderViewController alloc] init];
        [self.navigationController pushForm:self.navigationController pushToAndHidden:ActivityOrderVC];
    }
    
}



#pragma mark >>>>>>>>>>> 我的资产delegate
- (void)assertViewDidClick:(UIButton *)button
{
    //用户是否登录
    if (![JudgeUserLogInTool JudgeUserLonInStatus:self.view]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您尚未登录用户" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *SettingAction = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            LogInViewController *accountVC = [[LogInViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:accountVC];
            [self presentViewController:nav animated:YES completion:nil];
        }];
        [alert addAction:SettingAction];
        
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];
         return;
    }
    if (button.tag == 1992) {
        //充值账户
        NSLog(@"充值账户");
        
    }else if (button.tag == 1993){
        //积分余额
        MyStandingsViewController *myStandings = [[MyStandingsViewController alloc] init];
        myStandings.nameLabelString = [self.userInfoDict[@"data"] objectForKey:@"xingming"];
        [self.navigationController pushForm:self.navigationController pushToAndHidden:myStandings];
        
    }else if (button.tag == 1994){
        //总消费额
        NSLog(@"总消费");
        
    }else{
        //营业账户余额(商家)
        NSLog(@"营业账户");
    }
}

- (NSDictionary *)userInfoDict {
    if (!_userInfoDict) {
        _userInfoDict = [NSDictionary dictionary];
    }
    return _userInfoDict;
}

- (void)loadUserInfoNumberData {
    
    NSDictionary *params = @{
                             @"username":USERNAME
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppUser/UserInfo" completeBlock:^(id object) {
        self.userInfoDict = object;
        [self.tableView reloadData];
        NSLog(@"%@",self.userInfoDict);
    }];
}

- (void)dealloc
{
    NSLog(@"delloc");
}

@end
